<?php 

class LsyiiController
extends CController
{
	/**
	 * @var PluginYii_ActionYiiController
	 */
	protected $_ls_action;
	/**
	 * 
	 * @var string
	 */
	public $plugin;
	/**
	 * 
	 * @var CMenu
	 */
	public $menu;
	
	public $holdAction = true;
	protected $_last_action;
	
	public function init(){
		if(isset($GLOBALS['YII_LSACTION'])){
			$this->setLsAction($GLOBALS['YII_LSACTION']);
		}
		$this->createMenu();
	}
	
	public function createMenu(){
		$this->menu = Yii::app()->getWidgetFactory()->createWidget($this,'LsMenu');
	}
	
	/**
	 * @return PluginYii_ActionYiiController
	 */
	public function getLsAction(){
		return $this->_ls_action;
	}
	
	public function t($message,$params=array(),$language=null){
		return Yii::t("p_{$this->plugin}", $message, $params, 'messages', $language);
	}
	
	public function setLsAction($action){
		if($action instanceof PluginYii_ActionYiiController){
			$this->_ls_action = $action;
		}else{
			throw new CException('$action is not an instance of "PluginYii_ActionYiiController"');
		}
	}
	
	public function getViewPath(){
		return Yii::app()->getTemplatePath($this->plugin).DIRECTORY_SEPARATOR.$this->getId();
	}
	
	public function hookProcessOutput($args){
		$args['output'] = $this->processOutput($args['output']);
	}
	
	public function render($view,$data=null,$return=false){
		if($this->beforeRender($view))
		{
			$output=$this->renderPartial($view,$data,true);
			
			$this->afterRender($view,$output);
			
			if($return)
				return $output;
			else
				echo $output;
		}
	}
	
	
	public function smartyRender($view,$data=null,$return=false,$useControllerPath=true){
		if($this->beforeRender($view))
		{
			$path = ($useControllerPath ? $this->getViewPath().'/' : '')
				.$view;
			
			$output=Yii::E()->Viewer_YiiSmartyFetch($path, $data);
			
			$this->afterRender($view,$output);
			
			if($return)
				return $output;
			else
				echo $output;
		}
	}
	
	
	protected function afterAction($action){
		// register processOutput hook
		//Yii::E()->Hook_AddExecFunction('yii_yiiviewer_processoutput',array($this, 'hookProcessOutput'));
		// assign layout
		if(!($layout = $this->layout)){
			$layout = Yii::app()->layout;
		}
		if($layout && $lsAction = $this->getLsAction()){
			$lsAction->SetTemplateAction($layout);
		}
		// render and assign menu
		$this->runMenu();
	}
	
	
	public function menuOptions(){
		$this->menu->items = $this->getMenuItems();
	}
	
	public function getMenuItems(){
		return array();
	}
	
	public function renderMenu(){
		$this->menu->init();
		ob_start();
		$this->menu->run();
		return ob_get_clean();
	}
	
	public function runMenu(){
		$this->menuOptions();
		$menuContent = $this->renderMenu();
		$menuId = $this->plugin.'_'.$this->id;
		Yii::E()->Viewer_Assign('sYiiMenuContent', $menuContent);
		Yii::E()->Viewer_AddMenu(
			$menuId,
			Yii::app()->getTemplatePath('yii').'menu.tpl'
		);
		Yii::E()->Viewer_Assign('menu', $menuId);
	}
	
	public function actionIndex(){
		echo __METHOD__;
	}
	
	/**
	 * Runs the action after passing through all filters.
	 * This method is invoked by {@link runActionWithFilters} after all possible filters have been executed
	 * and the action starts to run.
	 * @param CAction $action action to run
	 */
	public function runAction($action)
	{
		$priorAction=parent::getAction();
		$this->setAction($action);
		if($this->beforeAction($action))
		{
			if($action->runWithParams($this->getActionParams())===false)
				$this->invalidActionParams($action);
			else{
				$this->afterAction($action);
				if($this->holdAction) $this->_last_action = $action;
			}
		}
		$this->setAction($priorAction);
	}
	
	public function getAction(){
		$action = parent::getAction();
		if($this->holdAction && !$action){
			$action = $this->_last_action;
		}
		return $action;
	}
	
}

?>